package cars;

import java.io.*;
import java.util.ArrayList;

public class Administrators extends Users {
    public Administrators() {
    }

    public Administrators(String userID, String userPWD, String userName) {
        super(userID, userPWD, userName);
    }

    @Override
    public String toString() {
        return super.toString();
    }

    public void AddCar(ArrayList<Vehicle> list, ArrayList<Users> users) throws IOException {
        System.out.print("请输入你要增加的汽车类型(轿车、客车、卡车)：");
        String type = sc.next();
        if (type.equals("轿车") || type.equals("客车") || type.equals("卡车")) {
            System.out.print("请输入你要加入车的品牌：");
            String brand = sc.next();
            String model = null;
            int seat = 0, ton = 0;
            if (type.equals("轿车")) {
                System.out.print("请输入你要加入车的类型：");
                model = sc.next();
            } else if (type.equals("客车")) {
                System.out.print("请输入你要加入的车的载客量：");
                model = sc.next();
                try {
                    seat = Integer.parseInt(model);
                } catch (NumberFormatException e) {
                    System.out.println("类型输入错误，应为数字！！");
                    return;
                }
            } else {
                System.out.print("请输入你要加入的车的载重：");
                try {
                    model = sc.next();
                    ton = Integer.parseInt(model);
                } catch (NumberFormatException e) {
                    System.out.println("类型输入错误，应为数字！！");
                    return;
                }
            }
            System.out.print("请输入车牌号：");
            String CarID = sc.next();
            for (Vehicle vehicle : list) {
                if (vehicle.getVehicleID().equals(CarID)) {
                    System.out.println("很抱歉，你要添加的车牌号已经存在了！！");
                    System.out.println("请重新选择");
                    return;
                }
            }
            String PerRent;
            double per;
            System.out.print("请输入车的日租金：");
            PerRent = sc.next();
            try {
                per = Double.parseDouble(PerRent);
            } catch (NumberFormatException e) {
                System.out.println("输入日租金类型错误!!!");
                return;
            }
            if (type.equals("轿车")) {
                Car c = new Car(type, brand, model, CarID, per);
                list.add(c);
            } else if (type.equals("客车")) {
                Bus b = new Bus(type, brand, seat, CarID, per);
                list.add(b);
            } else {
                Trunk t = new Trunk(type, brand, ton, CarID, per);
                list.add(t);
            }
            System.out.println("添加成功了！！！");
            BufferIn(list, users);
        }
    }

    public void ViewCar(ArrayList<Vehicle> list) {
        for (Vehicle vehicle : list) {
            System.out.println(vehicle);
        }
    }

    public void ChangeCar(ArrayList<Vehicle> list, ArrayList<Users> users) throws IOException {
        System.out.print("请输入你要修改车辆信息的车牌号：");
        String CarID = sc.next();
        for (Vehicle vehicle : list) {
            if (vehicle.getVehicleID().equals(CarID)) {
                if (vehicle instanceof Car) {
                    Car car = (Car)vehicle;
                    System.out.println("1.品牌 2.类型 3.车牌号 4.日租金");
                    String choose = sc.next();
                    switch (choose) {
                        case "1":
                            System.out.print("请输入更改后的品牌：");
                            String brand = sc.next();
                            car.setBrand(brand);
                            break;
                        case "2":
                            System.out.print("请输入更改后的类型：");
                            String type = sc.next();
                            car.setModel(type);
                            break;
                        case "3":
                            System.out.print("请输入更改后的车牌号：");
                            String ID = sc.next();
                            for (Vehicle vehicle1 : list) {
                                if (vehicle1.getVehicleID().equals(ID)) {
                                    System.out.println("该车牌号已经存在，修改失败");
                                    return;
                                }
                            }
                            car.setVehicleID(ID);
                            break;
                        case "4":
                            System.out.print("请输入更改后的日租金：");
                            String PerRent = sc.next();
                            double Per = 0;
                            try {
                                Per = Double.parseDouble(PerRent);
                            } catch (NumberFormatException e) {
                                System.out.println("输入类型错误，应输入数字！！！");
                                return;
                            }
                            car.setPerRent(Per);
                            break;
                        default:
                            System.out.println("没有该选项！");
                            return;
                    }
                    System.out.println("更改成功！");
                    BufferIn(list, users);
                    return;
                }else if(vehicle instanceof Bus){
                    Bus bus = (Bus) vehicle;
                    System.out.println("1.品牌 2.载客量 3.车牌号 4.日租金");
                    String choose = sc.next();
                    switch (choose) {
                        case "1":
                            System.out.print("请输入更改后的品牌：");
                            String brand = sc.next();
                            bus.setBrand(brand);
                            break;
                        case "2":
                            System.out.print("请输入更改后的载客量：");
                            String type = sc.next();
                            int seat;
                            try {
                                seat = Integer.parseInt(type);
                            } catch (NumberFormatException e) {
                                System.out.println("输入类型错误，应输入数字！！！");
                                return;
                            }
                            bus.setSeat(seat);
                            break;
                        case "3":
                            System.out.print("请输入更改后的车牌号：");
                            String ID = sc.next();
                            for (Vehicle vehicle1 : list) {
                                if (vehicle1.getVehicleID().equals(ID)) {
                                    System.out.println("该车牌号已经存在，修改失败");
                                    return;
                                }
                            }
                            bus.setVehicleID(ID);
                            break;
                        case "4":
                            System.out.print("请输入更改后的日租金：");
                            String PerRent = sc.next();
                            double Per = 0;
                            try {
                                Per = Double.parseDouble(PerRent);
                            } catch (NumberFormatException e) {
                                System.out.println("输入类型错误，应输入数字！！！");
                                return;
                            }
                            bus.setPerRent(Per);
                            break;
                        default:
                            System.out.println("没有该选项！");
                            return;
                    }
                    System.out.println("更改成功！");
                    BufferIn(list, users);
                    return;
                }else if (vehicle instanceof Trunk){
                    Trunk trunk = (Trunk) vehicle;
                    System.out.println("1.品牌 2.载重 3.车牌号 4.日租金");
                    String choose = sc.next();
                    switch (choose) {
                        case "1":
                            System.out.print("请输入更改后的品牌：");
                            String brand = sc.next();
                            trunk.setBrand(brand);
                            break;
                        case "2":
                            System.out.print("请输入更改后的载重：");
                            String type = sc.next();
                            int ton;
                            try {
                                ton = Integer.parseInt(type);
                            } catch (NumberFormatException e) {
                                System.out.println("输入类型错误，应输入数字！！！");
                                return;
                            }
                            trunk.setTonnage(ton);
                            break;
                        case "3":
                            System.out.print("请输入更改后的车牌号：");
                            String ID = sc.next();
                            for (Vehicle vehicle1 : list) {
                                if (vehicle1.getVehicleID().equals(ID)) {
                                    System.out.println("该车牌号已经存在，修改失败");
                                    return;
                                }
                            }
                            trunk.setVehicleID(ID);
                            break;
                        case "4":
                            System.out.print("请输入更改后的日租金：");
                            String PerRent = sc.next();
                            double Per = 0;
                            try {
                                Per = Double.parseDouble(PerRent);
                            } catch (NumberFormatException e) {
                                System.out.println("输入类型错误，应输入数字！！！");
                                return;
                            }
                            trunk.setPerRent(Per);
                            break;
                        default:
                            System.out.println("没有该选项！");
                            return;
                    }
                    System.out.println("更改成功！");
                    BufferIn(list, users);
                    return;
                }
            }
        }
        System.out.println("没有该车牌号！");
    }

    public void QueueCar(ArrayList<Vehicle> list, ArrayList<Users> users) throws IOException {
        System.out.print("请输入你要删除车辆的车牌号：");
        String ID = sc.next();
        for (Vehicle vehicle : list) {
            if (vehicle.getVehicleID().equals(ID)) {
                System.out.println(vehicle);
                while (true) {
                    System.out.println("是否删除 ");
                    System.out.println("是   否");
                    String choose = sc.next();
                    if (choose.equals("是")) {
                        list.remove(vehicle);
                        BufferIn(list, users);
                        System.out.println("删除成功");
                        return;
                    } else if (choose.equals("否")) {
                        return;
                    } else {
                        System.out.println("输入错误,请重新输入！！");
                    }
                }
            }
        }
        System.out.println("该车牌号不存在！！！");
    }

    public void ViewMoney() throws IOException {
        BufferedReader br = new BufferedReader(new FileReader("Allproperty"));
        double read = Double.parseDouble(br.readLine());
        System.out.println("营业额为：" + read);
        br.close();
    }

    public void AddMoney(ArrayList<Users> users, ArrayList<Vehicle> vehicles) throws IOException {
        while (true) {
            System.out.print("请输入充值的客户ID：");
            String ID = sc.next();
            for (Users user : users) {
                if (user.getUserID().equals(ID)) {
                    System.out.println(user);
                    System.out.println("请确认身份");
                    System.out.println("是   否");
                    String choose = sc.next();
                    if (choose.equals("是")) {
                        Customers c = (Customers) user;
                        double now = c.getMoney();
                        while (true) {
                            System.out.println("请输入你要充值的金额：");
                            String addStr = sc.next();
                            double add = 0;
                            try {
                                add = Double.parseDouble(addStr);
                            } catch (NumberFormatException e) {
                                System.out.println("输入格式错误，应为数字！！");
                                return;
                            }
                            if (add <= 0) {
                                System.out.println("充值的金额不可小于等于0");
                            } else {
                                double sum = now + add;
                                ((Customers) user).setMoney(sum);
                                // ((Customers) user).BufferInCar(Car, users);
                                System.out.println("充值成功");
                                BufferedReader br = new BufferedReader(new FileReader("Allproperty"));
                                double money = Double.parseDouble(br.readLine());
                                br.close();
                                money += add;
                                BufferedWriter bw = new BufferedWriter(new FileWriter("Allproperty"));
                                bw.write(String.valueOf(money));
                                bw.close();
                                return;
                            }
                        }
                    } else if (choose.equals("否")) {
                        System.out.println("请重新输入");
                    } else {
                        System.out.println("输入错误，请重新输入");
                    }
                }
            }
            System.out.println("无该用户，请重新输入");
        }
    }

    public void BufferIn(ArrayList<Vehicle> list, ArrayList<Users> users) throws IOException {
        Customers.Buff(list, users);
    }

}
